#!/usr/bin/env python
# -*- coding: utf-8 -*-

################################################################################
#
# Copyright (c) 2017. All Rights Reserved
#
################################################################################
"""
该模块对quora 重复文档进行识别，采用lstm+attention模型；

Authors: Fan Tao (fantao@mail.ustc.edu.cn)
Date:    2017/04/04 11:34:00
"""
from keras.models import load_model
import os
import codecs
import json
import gcc_omcp_utils as utils
import data_process

EMBED_DIM = 64
HIDDEN_DIM = 50
BATCH_SIZE = 128
NBR_EPOCHS = 50

MODEL_DIR = utils.BASE_DIR


def load_json(file):
    with open(file, "r", encoding='utf-8') as f:
        return json.load(f)


def load_seq_maxlen():
    with open(os.path.join(MODEL_DIR, "seq_maxlen.data"), "r", encoding="utf-8") as datafile:
        max_len = datafile.readline()
        return int(max_len)


if __name__ == '__main__':
    words = "为什么 APP 会 查询 一个 系统 失败"
    intention = "咨询 APP 异常"
    path = os.path.join(MODEL_DIR, "gcc-omcp-model.h5")
    model = load_model(filepath=path)
    # 读取测试文件，测试正确率。
    seq_maxlen = load_seq_maxlen()
    word2idx = load_json(os.path.join(MODEL_DIR, "word2idx.json"))
    result_list = list()
    test_file = "040102_test.txt"
    pred_out_file = "pred_out_file.txt"

    with codecs.open(os.path.join(MODEL_DIR, test_file), 'r', encoding="utf-8") as f:
        f.readline()
        total_success = 0
        total_error = 0
        index = 1
        for line in f:
            splits = line.split("\t")
            if len(splits) != 6:
                print(len(splits))
                print("error input:{0}".format(line))
                continue
            words = splits[3]
            intention = splits[4]
            excepted_result = splits[5].strip()
            words2vec, intention2vec = data_process.vectorize_predict_pair(words, intention, word2idx, seq_maxlen)
            classes = model.predict([words2vec, intention2vec], batch_size=BATCH_SIZE, verbose=0)
            # print(classes)
            pred = 1
            if classes[0][0] > classes[0][1]:
                pred = 0
            flag = "NO"
            if int(excepted_result) == pred:
                total_success = total_success + 1
                flag = "YES"
            else:
                total_error = total_error + 1
            result_list.append((words, intention, excepted_result, str(classes[0]), flag))
            if not index % 1000:
                if total_error:
                    acc_rate = total_success/(total_error+total_success)
                else:
                    acc_rate = 100
                print("total_success/(total_error+total_success={0}".format(acc_rate))
            index += 1
        print("final total_success/(total_error+total_success)={0}".format((total_success / (total_error + total_success))))

    # 保存测试结果
    with codecs.open(os.path.join(MODEL_DIR, pred_out_file), "w", encoding="utf-8") as fo:
        for i in range(len(result_list)):
            output_info = result_list[i]
            # words = output_info[0]
            # intention = output_info[1]
            # print("{0}\t{1}".format(words, intention))
            # print(output_info)
            fo.write("{0}\t{1}\t{2}\t{3}\t{4}\n".format(output_info[0], output_info[1], output_info[2], output_info[3],
                                                        output_info[4]))
    print("done!")
